package CustomResources;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import Main.Aplicacion;

public class CustomDialog2 extends JDialog {
	
	private static final long serialVersionUID = -1624718524787936862L;

	public static CustomDialog2 main(String titulo,String msj,String type,MouseAdapter listener,Aplicacion padre) {
		CustomDialog2 dialog = null;
		try {
			dialog = new CustomDialog2(titulo,msj,type,listener,padre);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dialog;
	}

	/**
	 * Create the dialog.
	 */
	public CustomDialog2(String titulo,String msj,final String type,MouseAdapter listener,Aplicacion padre) {
		
		super(padre, true);
		
		int xPadre=padre.getX();
		int yPadre=padre.getY();
		int wPadre=padre.getWidth();
		int hPadre=padre.getHeight();
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//		getContentPane().setBackground(Color.GRAY);
		setLocationRelativeTo(null);
		setBounds((xPadre+(wPadre-525)/2),(yPadre+(hPadre-295)/2),525,295);
		setResizable(false);
		getContentPane().setLayout(null);
		
		int anchoTitulo=titulo.length()*13;
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setOpaque(false);
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBounds((525-anchoTitulo-40)/2, 11, anchoTitulo+40, 44);
		getContentPane().add(panel);
		
		JLabel label = new JLabel(titulo);
//		label.setForeground(Color.WHITE);
		label.setFont(new Font("Calibri", Font.BOLD, 30));
		label.setBounds(20, 0, (anchoTitulo+100), 42);
		panel.add(label);
		
		JButton customButton_1 = new JButton("Aceptar");
		customButton_1.addMouseListener(listener);
		customButton_1.setBounds(256, 223, 118, 33);
		customButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				cancelar();
			}
		});
		getContentPane().add(customButton_1);
		
		JPanel panel_1 = new JPanel(){
			private static final long serialVersionUID = 7212317639655536455L;

			public void paintComponent (Graphics g){
				Dimension tamanio = getSize();
				ImageIcon imagenFondo=null;
				switch(type){
				case "Advertencia":
					imagenFondo = new ImageIcon(getClass().getResource("/Imagenes/warning_icon.png"));
					break;
				case "Error":
					imagenFondo = new ImageIcon(getClass().getResource("/Imagenes/error_icon.png"));
					break;
				case "Informacion":
					imagenFondo = new ImageIcon(getClass().getResource("/Imagenes/info_icon.png"));
					break;
				case "Confirmacion":
					imagenFondo = new ImageIcon(getClass().getResource("/Imagenes/confirm_icon.png"));
					break;
				case "Pregunta":
					imagenFondo = new ImageIcon(getClass().getResource("/Imagenes/question_icon.png"));
					break;
				}
				g.drawImage(imagenFondo.getImage(),0,0,tamanio.width, tamanio.height, null);
				setOpaque(false);
				super.paintComponent(g);
			}
		};
		panel_1.setBounds(50, 85, 108, 103);
		getContentPane().add(panel_1);
		
		JLabel label_1 = new JLabel("<html>"+msj+"</html>");
		label_1.setBounds(195, 70, 281, 120);
//		label_1.setForeground(Color.white);
		label_1.setFont(new Font("Calibri", Font.BOLD,23));
		getContentPane().add(label_1);
		
		JButton button = new JButton("Cancelar");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				cancelar();
			}
		});
		button.setBounds(391, 223, 118, 33);
		getContentPane().add(button);
		setVisible(true);
	}
	
	private void cancelar(){
		this.dispose();
	}
}
